//
//  MessageViewController.m
//  HAODE
//
//  Created by LiYinDi on 15/12/24.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "MessageViewController.h"
#import "RDVTabBarController.h"
#import "HttpRequest.h"
#import "MessageTableViewCell.h"
#import "SystemNotificationViewController.h"
#import "LatestRecommendViewController.h"
#import "TaskAlertViewController.h"
#import "TheLoginViewController.h"
#import "CustomServiceMessageViewController.h"
#import "MessageModel.h"
@interface MessageViewController ()

{
//    NSMutableArray *messageIconArray;
//    NSMutableArray *messageTimeArray;
//    NSMutableArray *messageContentArray;
//    NSMutableArray *messageTypeArray;
    NSArray *titleArray;
    NSArray *iconArray;
    
    NSMutableArray *modelArray;
    
}
@end

@implementation MessageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"消息";
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
     [[self rdv_tabBarController] setTabBarHidden:YES];
    
    modelArray=[NSMutableArray array];
   // messageContentArray=[NSMutableArray array];
//    messageIconArray=[NSMutableArray array];
   // messageTimeArray=[NSMutableArray array];
   // messageTypeArray=[NSMutableArray array];
    titleArray=[NSArray arrayWithObjects:@"任务提醒",@"最新推荐",@"客服消息",@"系统通知",@"报表推送", nil];
    iconArray=[NSArray arrayWithObjects:@"renwutixing_new",@"zuixintuijian_new",@"kefuxiaoxi_new",@"xitongshezhi_new",@"baobiaotuisong_new" ,nil];

    [self requestMessage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation];
    [self setAutoLayout];
    
}
-(void)setAutoLayout
{
   
    [_messageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        
        make.bottom.equalTo(self.view.mas_bottom) ;
        
        
        make.top.equalTo(self.view.mas_top) ;
    }];

}
-(void)requestMessage
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    [HttpRequest requestData:@"MsgCenter/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
       
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
//             NSLog(@"sssss===%@",severDataDict.dataDict);
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
 
                for (NSDictionary* dicc in arrr )
                {
                    MessageModel *model=[MessageModel dataModelWithDict:dicc];
//
                    [modelArray addObject:model];
                }
                [_messageTableView reloadData];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }

            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return modelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * const  ResultTableViewCellIdentifier = @"ResultTableViewCellIdentifier";
    MessageTableViewCell *cell=( MessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier: ResultTableViewCellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MessageTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model=modelArray[indexPath.row];
    
    if (iconArray.count>0) {
        cell.titleLabel.text=[titleArray objectAtIndex:indexPath.row];
        [cell.iconImageView setImage:[UIImage imageNamed:[iconArray objectAtIndex:indexPath.row]]];
    }
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   MessageModel *model=modelArray[indexPath.row];
    
    if ([model.msgType isEqualToString:@"SystemNotice"]) {
        SystemNotificationViewController *systemController = [[SystemNotificationViewController alloc] init];
        [self.navigationController pushViewController:systemController animated:YES];
    }
   else if ([model.msgType isEqualToString:@"Recommend"]) {
        LatestRecommendViewController *recommendController = [[LatestRecommendViewController alloc] init];
        [self.navigationController pushViewController:recommendController animated:YES];
    }
    else if ([model.msgType isEqualToString:@"TaskRemind"])
    {
        TaskAlertViewController *alertController = [[TaskAlertViewController alloc] init];
        [self.navigationController pushViewController:alertController animated:YES];
    }
    else if ([model.msgType isEqualToString:@"CustomerService"])
    {
        CustomServiceMessageViewController *customVC = [[CustomServiceMessageViewController alloc] init];
        [self.navigationController pushViewController:customVC animated:YES];
    }
}


-(void)setNavigation
{
//    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 23)];
    titleLabel.text=@"消息中心";
//    titleLabel.font=[UIFont systemFontOfSize: 19];
    self.navigationItem.titleView=titleLabel;

}
-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
